/**
 * 增加一行信息
 */
function addRow(){
   var root = document.getElementById("tbody");
   var allRows = root.getElementsByTagName('tr');
   var cRow = allRows[1].cloneNode(true);
   cRow.style.display="";
   root.appendChild(cRow);
   //selectCurrencys(cRow);
}

/**
 * 删除一行信息
 */
function removeRow(r)
{
    var root = r.parentNode;
    var allRows = root.getElementsByTagName('tr');
    if(allRows.length > 2){   	
       root.removeChild(r);
    }

    var itemName = document.getElementsByName("itemName") ;
    if(itemName.length == 1){
		document.getElementById("comboxWithTree").style.display = "";
		document.getElementById("nodeText").style.display = "none";
		document.getElementById('projectName').disabled = false; 
     }
    zjs(); 
}

/**
 * 验证借款金额输入的必须是数字
 */
function yzsz(amount, r){
	if(isNaN(amount.value)||amount.value-0<0){
		alert("请输入数字并且大于0！");
		amount.value="";
		return;
	}
	zjs(); 
}

/**
 * 验证借款外币金额输入的必须是数字
 */
function yzwbsz(amount, r){
	var tab = document.getElementsByName("cName") ;
	if(isNaN(amount.value) || amount.value - 0 < 0){
		alert("请输入数字并且大于0！");
		amount.value="";
		return;
	}
	
	if (tab[r.rowIndex-1].value != null && tab[r.rowIndex-1].value != "") {
		var cName = tab[r.rowIndex-1].value;
		currencyRate(r, cName.split(",")[0]);
	} else {
		alert("请您先选择外币币种！");
		amount.value="";
		selectCurrencys(r);
		return;
	}
	zjs(); 
}

/**
 * 计算总金额
 */
function zjs(){
 var tab1 = document.getElementsByName("amount") ;
 var amount = 0;

 for(var i = 1; i < tab1.length; i++){
   amount += Number(tab1[i].value);
 }

 var reimbTotal = Number(amount);
 document.getElementById("reimbTotal").value = reimbTotal.toFixed(2);

}

/**
 * 点击项目类别后加载所有的类别信息
 */
 function selectCurrencys(r){
   var tab = document.getElementsByName("cName") ;
   showCurrencys(tab[r.rowIndex-1]);	
 }

/**
 * 加载所有的类别信息
 */
function showCurrencys(rr){
	var r = rr.parentNode.parentNode;
		Ext.Ajax.request({
			url : '/reimb/getCurrencys.do',
			params : {
			},
			method : 'POST',
			success : function(response) {
			    var jsonResult = Ext.util.JSON.decode(response.responseText);
			        var result = jsonResult.result ;
			        var currencys = result.split(":");
			        var html = "<select name=\'cId\' onchange=\'currencyChange(this)\' style=\'width: 90px\'>";
			        var currency;
			        for(var i = 0; i < currencys.length - 1; i++){
			        	currency = currencys[i].split(",");
			           	html = html + "<option value=\'" + currencys[i] + "\' ";
			           	if(rr.value == currency[1]){
			           		html = html + " selected=\'selected\' ";
			           	}
			
			           	html = html + " >" + currency[1] +"</option>";
			           }
			        rr.parentNode.innerHTML=html+"</select><input name=\'cName\' type=\'hidden\'/>";
			    }
		});
}

/**
 * 加载币种汇率信息
 */
function currencyRate(r, currencyId){
	var cId = document.getElementsByName("cId");
	var currencyId = cId[r.rowIndex-1].value.split(",")[0];
		Ext.Ajax.request({
			url : '/reimb/getCurrencyRate.do',
			params : {
				currencyId : currencyId
			},
			method : 'POST',
			success : function(response) {
			    var jsonResult = Ext.util.JSON.decode(response.responseText);
			        var result = jsonResult.result ;
			        
			        if (result == '0.0' || result == 'failure') {
				        alert("无法获取汇率，请您检查该币种汇率表中的数据！");
			        }
			        if (result != null && result != 'failure') {
			          var tab = document.getElementsByName("amount") ;	
			       	  var tab1 = document.getElementsByName("exchangeAmount");
			       	  //根据外币金额和币种的汇率计算兑换的本币金额
                     var amount = Number(tab1[r.rowIndex-1].value) * Number(result); 
                     tab[r.rowIndex-1].value = amount.toFixed(2);
                     zjs();                     
			        } else if (result == null) {
			          tab1[r.rowIndex-1].value = "";
			        } 
			    }
		});
}

/**
 * 根据选择的币种变更币种信息
 */
function currencyChange(currency){
 var r = currency.parentNode.parentNode;
 var cName = document.getElementsByName("cName") ;
 var amount = document.getElementsByName("amount") ;
 var exchangeAmount = document.getElementsByName("exchangeAmount") ;
 if (currency.value.split(",")[0] == "") {
	 amount[r.rowIndex-1].value = "";
	 exchangeAmount[r.rowIndex-1].value = "";
	 zjs(); 
	 return false;
 }
 cName[r.rowIndex-1].value = currency.value;
 currencyRate(r, cName[r.rowIndex-1].value.split(",")[0]);
}